﻿using System;
using System.Configuration;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using MySql.Data.MySqlClient;

namespace TMS
{
    public partial class CreateEditVehicleControl : UserControl
    {
        public MySqlDataAdapter MyDA = new MySqlDataAdapter();
        bool edit = false;
        int ID = 0;
        DataGridViewRow editParameters = null;

        public CreateEditVehicleControl()
        {
            Program.GetMainForm.Text = "TMS - Ново возило";
            InitializeComponent();
            
        }

        public CreateEditVehicleControl(DataGridViewRow parameters)
        {
            //THIS IS THE EDIT VIEW
            Program.GetMainForm.Text = "TMS - Возило: " + parameters.Cells[1].Value.ToString() + ", " + parameters.Cells[2].Value.ToString();
            InitializeComponent();
            this.edit = true;

            txtModel.Text = parameters.Cells[1].Value.ToString();
            txtRegistration.Text = parameters.Cells[2].Value.ToString();
            txtDriver.Text = parameters.Cells[3].Value.ToString();

            ID = Convert.ToInt32(parameters.Cells[0].Value);

            editParameters = (DataGridViewRow)parameters.Clone();
            for (Int32 index = 0; index < parameters.Cells.Count; index++)
            {
                editParameters.Cells[index].Value = parameters.Cells[index].Value;
            }
        }

        
        private void toolStripBack_Click(object sender, EventArgs e)
        {
            if (Program.GetMainForm.Back())
                this.Dispose();
        }

        private void toolStripForward_Click(object sender, EventArgs e)
        {
            if (Program.GetMainForm.Forward())
                this.Dispose();
        }

        private void toolStripDeleteVehicle_Click(object sender, EventArgs e)
        {
            if (MessageBox.Show("Сигурни сте дека сакате да го избришете возилото?", "Потврда за бришење", MessageBoxButtons.YesNo) == DialogResult.Yes)
            {
                Vehicle vehicle = new Vehicle(ID);
                vehicle.DeleteVehicleFromDB();
                
                Program.GetMainForm.ShowControl(ControlsEnum.VEHICLES_CONTROL);
                this.Dispose();
            }
        }

        private void toolStripSaveVehicle_Click(object sender, EventArgs e)
        {

            Vehicle vehicle = new Vehicle(ID, txtModel.Text, txtRegistration.Text, txtDriver.Text);

            if (edit)
            {
                if (editParameters.Cells[2].Value.ToString() != txtRegistration.Text && vehicle.CheckExistingVehicleInDB())
                {
                    MessageBox.Show("Веќе постои возило со ист регистарски број!");
                }
                else
                {
                    vehicle.UpdateVehicleInDB();
                    this.Hide();
                    Program.GetMainForm.registerInHistoryMain(ControlsEnum.CREATE_EDIT_VEHICLES_CONTROL, editParameters);
                    Program.GetMainForm.ShowControl(ControlsEnum.VEHICLES_CONTROL);
                }
            }
            else {

                if (vehicle.CheckExistingVehicleInDB())
                {
                    MessageBox.Show("Веќе постои возило со ист регистарски број!");
                }
                else 
                {
                    vehicle.InsertVehicleInDB();
                    this.Hide();
                    Program.GetMainForm.registerInHistoryMain(ControlsEnum.CREATE_EDIT_VEHICLES_CONTROL, editParameters);
                    Program.GetMainForm.ShowControl(ControlsEnum.VEHICLES_CONTROL);
                }
            }


            //if (edit && editParameters.Cells[2].Value.ToString() == txtRegistration.Text && vehicle.CheckExistingVehicleInDB())
            //{
            //    MessageBox.Show("Веќе постои возило со ист регистарски број!");
            //}
            //else
            //{
            //    if (edit)
            //    {
            //        vehicle.UpdateVehicleInDB();
            //    }
            //    else
            //    {
            //        vehicle.InsertVehicleInDB();
            //    }

            //    this.Hide();
            //    Program.GetMainForm.registerInHistoryMain(ControlsEnum.CREATE_EDIT_VEHICLES_CONTROL, editParameters);
            //    Program.GetMainForm.ShowControl(ControlsEnum.VEHICLES_CONTROL);
            //}

        }

        private void toolStripSaveAsNewVehicle_Click(object sender, EventArgs e)
        {
            Vehicle vehicle = new Vehicle(ID, txtModel.Text, txtRegistration.Text, txtDriver.Text);

            if (vehicle.CheckExistingVehicleInDB())
            {
                MessageBox.Show("Веќе постои возило со ист регистарски број!");
            }
            else
            {
                vehicle.InsertVehicleInDB();

                MessageBox.Show("Возилото е успешно внесено.");

                this.Hide();

                Program.GetMainForm.ShowControl(ControlsEnum.VEHICLES_CONTROL);
            }
        }

    }
}
